package com.smiier.skin.ui;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.TextView;
import cn.o.app.event.listener.OnSelectedChangeListener;
import cn.o.app.json.JsonUtil;
import cn.o.app.net.INetTask;
import cn.o.app.net.NetTaskListener;
import cn.o.app.ui.FlowLayout;
import cn.o.app.ui.OTab;
import cn.skinapp.R;

import com.evan.common.pullrefresh.PullToRefreshBase;
import com.evan.common.pullrefresh.PullToRefreshBase.OnRefreshListener;
import com.evan.common.pullrefresh.PullToRefreshListView;
import com.evan.common.utils.CacheCommon;
import com.evan.common.utils.CommonUtility;
import com.pifuke.adapter.TreatmentProjectListAdapter;
import com.smiier.skin.DoctorDetailActivity;
import com.smiier.skin.MulitLevelDataChooseActivity;
import com.smiier.skin.SearchActivity;
import com.smiier.skin.TreatmentDetailActivity;
import com.smiier.skin.adapter.UserAdapter;
import com.smiier.skin.constant.Constant;
import com.smiier.skin.extra.TreatmentProjectItemExtra;
import com.smiier.skin.net.DoctorTreatmentProjectListTask.DoctorTreatmentProjectItem;
import com.smiier.skin.net.DoctorUserGetListTask;
import com.smiier.skin.net.DoctorUserGetListTask.DoctorUserGetListRequest;
import com.smiier.skin.net.DoctorUserGetListTask.DoctorUserGetListResponse;
import com.smiier.skin.net.SymptomTask;
import com.smiier.skin.net.SymptomTask.SymptomRequest;
import com.smiier.skin.net.SymptomTask.SymptomResponse;
import com.smiier.skin.net.TreatmentProjectListTask;
import com.smiier.skin.net.TreatmentProjectListTask.TreatmentProjectListRequest;
import com.smiier.skin.net.TreatmentProjectListTask.TreatmentProjectListResponse;
import com.smiier.skin.utils.Util;

public class DoctorFragment extends BasicFragment implements
		OnRefreshListener<ListView>, OnItemClickListener, View.OnClickListener {

	private Button current_btn;

	private PullToRefreshListView list_morentuijian;
	private PullToRefreshListView mListTreatments;

	private UserAdapter morentuijianAdapter, haopingduAdapter, filterAdapter;

	private final int TYPE_MORENTUIJIAN = 1, TYPE_HAOPINGDU = 0,
			TYPE_FILTER = 2;

	private int type = TYPE_MORENTUIJIAN;
	private OTab mOtab;
	long start_qid_morentuijian, start_qid_haopingdu, start_qid_filter;
	long start_qid;
	// private LinearLayout layout_img_empty;
	private int mIndex = 0;
	ArrayList<Object> array_morentuijian = new ArrayList<Object>(),
			array_haopingdu = new ArrayList<Object>(),
			array_filter = new ArrayList<Object>();
	private View mListHead;
	protected DoctorUserGetListRequest mRequest = new DoctorUserGetListRequest();
	private PopupWindow mPop;
	private View mPopView;
	private TextView mGetPopLocation;
	int[] array = new int[2];
	int[] array1 = new int[2];
	private int mScreenWidth;
	private TextView mDefault;
	private TextView mGood;
	private boolean mDoctorList = true;
	private TreatmentProjectListAdapter mTreatmentAdapter;
	private View mTreatmentHead;
	private TextView mTreatmentType;
	private Drawable mArrowUp, mArrowDown;
	private PopupWindow mTreatmentPop;
	View mTreatview;

	public DoctorFragment(Context context) {
		super(context);
	}

	public DoctorFragment(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public DoctorFragment(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	@Override
	public void onCreate() {
		super.onCreate();
		this.setContentView(R.layout.fragment_doctor);
		mPop = Util.getPopWindow(getContext());
		mPopView = LayoutInflater.from(getContext()).inflate(
				R.layout.doctor_select, null);
		mPop.setContentView(mPopView);
		Resources mRes = getContext().getResources();
		mTreatmentPop = Util.getPopWindow(getContext());
		mTreatview = LayoutInflater.from(getContext()).inflate(
				R.layout.treament_select, null);
		mTreatview.findViewById(R.id.treatment_select0)
				.setOnClickListener(this);
		mTreatview.findViewById(R.id.treatment_select1)
				.setOnClickListener(this);
		mTreatview.findViewById(R.id.treatment_select2)
				.setOnClickListener(this);
		mTreatview.findViewById(R.id.treatment_select3)
				.setOnClickListener(this);
		mTreatview.findViewById(R.id.treatment_select4)
				.setOnClickListener(this);
		mTreatmentPop.setContentView(mTreatview);
		mArrowUp = mRes.getDrawable(R.drawable.arrow_up_green);
		mArrowDown = mRes.getDrawable(R.drawable.arrow_down_green);
		mArrowDown.setBounds(0, 0, mArrowDown.getIntrinsicWidth(),
				mArrowDown.getIntrinsicHeight());
		mArrowUp.setBounds(0, 0, mArrowUp.getIntrinsicWidth(),
				mArrowUp.getIntrinsicHeight());
		mDefault = (TextView) mPopView.findViewById(R.id.default_list);
		mGood = (TextView) mPopView.findViewById(R.id.good_list);
		mDefault.setOnClickListener(this);
		mGood.setOnClickListener(this);
		init();
		setNavTitle("医生");
		setNavLeftBtnDrawable(R.drawable.icon_question_type);
		setNavRightBtnDrawable(R.drawable.btn_search);
		back.setOnClickListener(this);
		text_right.setOnClickListener(this);
		list_morentuijian = findViewById(R.id.list_morentuijian,
				PullToRefreshListView.class);
		mListTreatments = findViewById(R.id.list_treatment,
				PullToRefreshListView.class);
		mTreatmentAdapter = new TreatmentProjectListAdapter();
		mTreatmentHead = LayoutInflater.from(getContext()).inflate(
				R.layout.treatment_header, null);

		mListTreatments.getListView().addHeaderView(mTreatmentHead);

		mListHead = LayoutInflater.from(getApplicationContext()).inflate(
				R.layout.doctor_header, null);
		mTreatmentType = (TextView) mTreatmentHead
				.findViewById(R.id.treatment_type);
		mTreatmentType.setOnClickListener(this);
		mGetPopLocation = (TextView) mListHead.findViewById(R.id.pop_location);
		ListView list = list_morentuijian.getListView();

		list.setDivider(getResources().getDrawable(
				R.drawable.border_blue_drawable));
		list.setDividerHeight(1);

		list.addHeaderView(mListHead);
		mOtab = findViewById(R.id.tab, OTab.class);

		mOtab.setOnSelectedChangeListener(new OnSelectedChangeListener() {

			@Override
			public void onChanged(View v, int index) {
				if (index == 0) {
					setNavLeftBtnShow(true);
					list_morentuijian.setVisibility(View.VISIBLE);
					mListTreatments.setVisibility(View.GONE);
					list_morentuijian.setContextEmptyType(false, 2);
					mIndex = 0;
					type = TYPE_MORENTUIJIAN;
					mRequest.sort = 0;
					mDoctorList = true;
					if (type == TYPE_MORENTUIJIAN
							&& CommonUtility.isNull(morentuijianAdapter)) {

					} else if (type == TYPE_HAOPINGDU
							&& CommonUtility.isNull(haopingduAdapter)) {
					}
					list_morentuijian.doPullRefreshing(true);
				} else if (index == 1) {
					setNavLeftBtnShow(false);
					list_morentuijian.setVisibility(View.GONE);
					mListTreatments.setVisibility(View.VISIBLE);

					mDoctorList = false;
					mListTreatments.doPullRefreshing(true);
					loadTreatmentData();
				}

			}
		});
		mTreatmentPop.setOnDismissListener(new OnDismissListener() {

			@Override
			public void onDismiss() {
				mTreatmentType.setCompoundDrawables(null, null, mArrowDown,
						null);
			}
		});
		/*
		 * list = list_haopingdu.getListView();
		 * list.setDivider(getResources().getDrawable(
		 * R.drawable.border_blue_drawable)); list.setDividerHeight(1);
		 */
		list_morentuijian.setOnRefreshListener(this);
		list_morentuijian.setPullRefreshEnabled(false);
		list_morentuijian.setPullLoadEnabled(false);
		list_morentuijian.setScrollLoadEnabled(true);
		list_morentuijian.doPullRefreshing(true);
		list_morentuijian.setOnItemClickListener(this);

		mListTreatments.setOnRefreshListener(this);
		mListTreatments.setPullRefreshEnabled(false);
		mListTreatments.setPullLoadEnabled(false);
		mListTreatments.setScrollLoadEnabled(true);
		mListTreatments.setAdapter(mTreatmentAdapter);
		mListTreatments.doPullRefreshing(true);
		mListTreatments.setOnItemClickListener(this);

		// list_morentuijian.hideDivider();
		// list_haopingdu.hideDivider();

		// layout_img_empty = findViewById(R.id.layout_img_empty,
		// LinearLayout.class);

		mRequest.pagesize = 10;
		mRequest.sort = 1;
		type = 1;
	}

	@Override
	public void onResume() {
		super.onResume();
		list_morentuijian.pullHeaderLayout(180);
		mListTreatments.pullHeaderLayout(180);
		mListTreatments.doPullRefreshing(true);
		list_morentuijian.doPullRefreshing(true);
		if (mIndex == 0) {
			mIndex = 0;
			type = TYPE_MORENTUIJIAN;
			mRequest.sort = 1;
			list_morentuijian.doPullRefreshing(true);
			list_morentuijian.setContextEmptyType(false, 2);
		}
	}

	@Override
	public boolean onInterceptBackPressed() {
		if (CommonUtility.indexOf((Activity) getContext(), view_filter) != -1) {
			CommonUtility.removeView((Activity) getContext(), view_filter,
					R.anim.push_left_out);
			return true;
		}
		return super.onInterceptBackPressed();
	}

	View view_filter;
	Button btn_address_all, btn_address_choose, btn_good_at_all,
			btn_good_at_zuochuang, btn_good_at_pizhen;
	ArrayList<Button> list_fileter_address = new ArrayList<Button>();
	ArrayList<CheckBox> list_filter_goodat = new ArrayList<CheckBox>();

	FlowLayout lbl_filter;

	void oSkilled(JSONArray array) {
		for (int i = 0; i < array.length(); i++) {
			try {
				String skill = array.getJSONObject(i).getString("Name");
				CheckBox button = (CheckBox) LayoutInflater.from(getContext())
						.inflate(R.layout.item_filter_button, null);
				button.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						onDynamicFilter(v);
					}
				});
				button.setText(skill);
				lbl_filter.addView(button);
				list_filter_goodat.add(button);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public void onDynamicFilter(View view) {
		btn_good_at_all.setBackgroundResource(R.drawable.btn_filter_unselected);
		btn_good_at_all.setTextColor(0xff609381);
	}

	public void onClick(View view) {
		int id = view.getId();
		if (id == R.id.treatment_select1) {
			req.category = "1";
			mTreatmentType.setText("注射微整");
			mTreatmentPop.dismiss();
			mListTreatments.setHasMoreData(true);
			mTreatmentAdapter.setDataProvider(null);
			mTreatmentAdapter.notifyDataSetChanged();
			loadTreatmentData();
		} else if (id == R.id.treatment_select2) {
			req.category = "2";
			mTreatmentType.setText("化学换肤");
			mTreatmentPop.dismiss();
			mListTreatments.setHasMoreData(true);
			loadTreatmentData();
			mTreatmentAdapter.setDataProvider(null);
			mTreatmentAdapter.notifyDataSetChanged();
		} else if (id == R.id.treatment_select3) {
			req.category = "3";

			mTreatmentType.setText("光子嫩肤");
			mTreatmentPop.dismiss();
			mListTreatments.setHasMoreData(true);
			loadTreatmentData();
			mTreatmentAdapter.setDataProvider(null);
			mTreatmentAdapter.notifyDataSetChanged();
		} else if (id == R.id.treatment_select4) {
			req.category = "4";
			mTreatmentType.setText("激光美肤");
			mTreatmentPop.dismiss();
			mListTreatments.setHasMoreData(true);
			loadTreatmentData();
			mTreatmentAdapter.setDataProvider(null);
			mTreatmentAdapter.notifyDataSetChanged();
		} else if (id == R.id.treatment_select0) {
			req.category = "";
			mTreatmentType.setText("项目类型");
			mTreatmentPop.dismiss();
			mListTreatments.setHasMoreData(true);
			loadTreatmentData();
			mTreatmentAdapter.setDataProvider(null);
			mTreatmentAdapter.notifyDataSetChanged();
		} else if (id == R.id.treatment_type) {
			mTreatmentType.setCompoundDrawables(null, null, mArrowUp, null);
			mTreatmentHead.getLocationOnScreen(array);

			mTreatview
					.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
			mTreatmentPop.setWidth(mTreatview.getMeasuredWidth());
			mTreatmentPop.setHeight(mTreatview.getMeasuredHeight());
			mTreatmentPop.showAtLocation(mTreatmentHead, Gravity.RIGHT
					| Gravity.TOP, com.smiier.skin.utils.CommonUtility.dip2px(
					getContext(), 12), array[1] + mTreatmentHead.getBottom());
		} else if (id == R.id.btn_left) {
			// 点击左上角按钮
			if (CommonUtility.isNull(view_filter)) {
				view_filter = LayoutInflater.from(getContext()).inflate(
						R.layout.view_filiter_doctor, null);
				view_filter.findViewById(R.id.rl_filter_root)
						.setOnClickListener(this);
				view_filter.findViewById(R.id.btn_reset_setting)
						.setOnClickListener(this);
				view_filter.findViewById(R.id.btn_ok).setOnClickListener(this);
				btn_address_all = (Button) view_filter
						.findViewById(R.id.btn_address_all);
				btn_address_all.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						onFilter(v);
					}
				});
				btn_address_choose = (Button) view_filter
						.findViewById(R.id.btn_address_choose);
				btn_address_choose.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						onFilter(v);
					}
				});
				list_fileter_address.add(btn_address_all);
				list_fileter_address.add(btn_address_choose);
				btn_good_at_all = (Button) view_filter
						.findViewById(R.id.btn_good_at_all);
				btn_good_at_all.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						onFilter(v);
					}
				});
				lbl_filter = (FlowLayout) view_filter
						.findViewById(R.id.lbl_filter);

				ArrayList<CacheCommon> caches = dbUtil.queryCache("Skilled");
				if (caches.size() > 0) {
					String data = caches.get(0).data;
					try {
						oSkilled(new JSONArray(data));
					} catch (Exception e) {
						e.printStackTrace();
					}
				} else {
					SymptomRequest req = new SymptomRequest();
					SymptomTask task = new SymptomTask();
					task.setRequest(req);
					task.addListener(new NetTaskListener<SymptomTask.SymptomRequest, SymptomTask.SymptomResponse>() {

						@Override
						public void onException(
								INetTask<SymptomRequest, SymptomResponse> task,
								Exception e) {

						}

						@Override
						public void onComplete(
								INetTask<SymptomRequest, SymptomResponse> task,
								SymptomResponse response) {
							try {
								String o = JsonUtil.convert(response);
								JSONArray array = new JSONArray(o);
								oSkilled(array);
								dbUtil.saveOrUpdate("Skilled", o);
							} catch (Exception e) {

							}
						}
					});
					add(task);
				}
			}
			CommonUtility.addView((Activity) getContext(), view_filter,
					R.anim.push_left_in);
		} else if (id == R.id.text_right) {
			Intent intent = new Intent(getContext(), SearchActivity.class);
			startActivity(intent);
		} else if (id == R.id.rl_filter_root) {
			if (!CommonUtility.isNull(view_filter)) {
				CommonUtility.removeView((Activity) getContext(), view_filter,
						R.anim.push_left_out);
			}
		} else if (id == R.id.btn_reset_setting) {
			btn_address_all.performClick();
			btn_good_at_all.performClick();
			list_morentuijian.doPullRefreshing(true);
			setNavLeftBtnDrawable(R.drawable.btn_slide_menu);
			setNavTitle("医生");
			CommonUtility.removeView((Activity) getContext(), view_filter,
					R.anim.push_left_out);
		} else if (id == R.id.btn_ok) {
			filterAdapter = null;
			// mRequest.Symptoms = new ArrayList<String>();
			StringBuilder sb = new StringBuilder();
			for (CheckBox box : list_filter_goodat) {
				if (box.isChecked()) {
					sb.append(box.getText().toString()).append("#");
					// mRequest.Symptoms.add(box.getText().toString());
				}
			}
			if (sb.length() <= 1) {
				mRequest.skilled = "";
			} else {
				mRequest.skilled = sb.replace(sb.length() - 1, sb.length(), "")
						.toString();
			}
			// if (mRequest.Symptoms.size() == 0) {
			// mRequest.Symptoms = null;
			// }
			if (!CommonUtility.isNull(filterAdapter)) {
				array_filter.clear();
				filterAdapter.notifyUpdate();
			}

			type = TYPE_FILTER;
			start_qid_filter = 0;
			loadData(false);
			setNavLeftBtnDrawable(R.drawable.btn_slide_menu_filter);
			setNavTitle("筛选结果");
			CommonUtility.removeView((Activity) getContext(), view_filter,
					R.anim.push_left_out);
		}

		else if (id == R.id.default_list) {
			type = TYPE_MORENTUIJIAN;
			// Default = 0 表示是否获取推荐用户列表 1 为获取默认推荐 0
			// 为不推荐 可不传，不传为不推荐用户
			morentuijianAdapter = null;
			mRequest.sort = 1;
			type = 1;
			start_qid = 0;
			mGetPopLocation.setText("默认推荐");
			loadData(false);

		} else if (id == R.id.good_list) {
			haopingduAdapter = null;
			type = TYPE_HAOPINGDU;
			mRequest.sort = 0;
			start_qid = 0;
			type = 0;
			mGetPopLocation.setText("好评度");
			loadData(false);
		}
		if (mPop.isShowing()) {
			mPop.dismiss();
		}

	}

	public void onFilter(View view) {
		int id = view.getId();
		if (id == R.id.btn_address_all) {
			mRequest.city = null;
			btn_address_choose.setText("请选择");
			resetBtnStatus(list_fileter_address, view);
		} else if (id == R.id.btn_address_choose) {
			/*
			 * mRequest.c = new ArrayList<Integer>(); mRequest.statuses.add(1);
			 */
			Intent intent = new Intent(getContext(),
					MulitLevelDataChooseActivity.class);
			String[] datas = { Constant.URL.REQUEST_HOSPITAL_JSON, "选择省份",
					"Hospital" };
			intent.putExtra(Constant.IDENTITY_KEY, datas);
			intent.putExtra(Constant.IDENTITY_KEY_1, true);
			startActivityForResult(intent, 0);
			resetBtnStatus(list_fileter_address, view);
		} else if (id == R.id.btn_good_at_all) {
			mRequest.skilled = null;
			for (CheckBox checkBox : list_filter_goodat) {
				checkBox.setChecked(false);
			}
			Button btn = (Button) view;
			btn.setBackgroundResource(R.drawable.btn_filter_selected);
			btn.setTextColor(getResources().getColor(R.color.white));
		}
	}

	void resetBtnStatus(ArrayList<Button> btns, View view) {
		Button b = (Button) view;
		for (Button btn : btns) {
			btn.setBackgroundResource(R.drawable.btn_filter_unselected);
			btn.setTextColor(0xff609381);
		}
		b.setBackgroundResource(R.drawable.btn_filter_selected);
		b.setTextColor(getResources().getColor(R.color.white));
	}

	@Override
	public void onItemClick(AdapterView<?> adapteView, View view, int pos,
			long arg3) {
		if (mDoctorList) {
			if (pos == 0) {
				mListHead.getLocationOnScreen(array);
				mGetPopLocation.measure(MeasureSpec.UNSPECIFIED,
						MeasureSpec.UNSPECIFIED);
				mGetPopLocation.getLocationOnScreen(array1);
				mPopView.measure(MeasureSpec.UNSPECIFIED,
						MeasureSpec.UNSPECIFIED);
				mPop.setWidth((mListHead.getWidth() + array[0]) / 3);
				mPop.setHeight(mPopView.getMeasuredHeight());
				mPop.showAtLocation(this, Gravity.TOP, mListHead.getWidth() - 2
						* (mListHead.getWidth() + array[0]) / 3, array1[1]
						+ mGetPopLocation.getBottom());
			} else {
				JSONObject object = (JSONObject) view.getTag();
				Intent intent = new Intent(getContext(),
						DoctorDetailActivity.class);
				intent.putExtra(Constant.IDENTITY_KEY, object.toString());
				startActivity(intent);
			}
		} else {
			if (pos == 0) {

			} else {
				DoctorTreatmentProjectItem item = (DoctorTreatmentProjectItem) mTreatmentAdapter
						.getDataProvider().get((int) arg3);
				Intent i = new Intent(getContext(),
						TreatmentDetailActivity.class);
				TreatmentProjectItemExtra extra = new TreatmentProjectItemExtra();
				extra.setUser(item);
				if (extra.putTo(i)) {
					startActivity(i);
				}
			}
		}
	}

	/**
	 * method desc：
	 */
	public void loadData(boolean isShowDialog) {
		/*
		 * if (type == TYPE_MORENTUIJIAN) { start_qid = start_qid_morentuijian;
		 * } else if (type == TYPE_HAOPINGDU) { start_qid = start_qid_haopingdu;
		 * } else if (type == TYPE_FILTER) { start_qid = start_qid_filter; }
		 */
		// mRequest.token = GlobalSettings.sToken;
		mRequest.start = start_qid;
		mRequest.pagesize = 10;
		DoctorUserGetListTask task = new DoctorUserGetListTask();
		task.setRequest(mRequest);
		task.addListener(new NetTaskListener<DoctorUserGetListTask.DoctorUserGetListRequest, DoctorUserGetListTask.DoctorUserGetListResponse>() {

			@Override
			public void onException(
					INetTask<DoctorUserGetListRequest, DoctorUserGetListResponse> task,
					Exception e) {
				e.printStackTrace();
				list_morentuijian.setContextEmptyType(false, 2);
				list_morentuijian.doComplete();
			}

			@Override
			public void onComplete(
					INetTask<DoctorUserGetListRequest, DoctorUserGetListResponse> task,
					DoctorUserGetListResponse response) {
				list_morentuijian.doComplete();
				if (response.ResultCode != 200) {

					return;
				} else {
					try {
						if (array_morentuijian.size() < 1) {
							list_morentuijian.setContextEmptyType(true, 1);
						} else {
							list_morentuijian.setContextEmptyType(false, 1);
						}
						o(new JSONArray(JsonUtil.convert(response.Res.Users)));
						if (array_morentuijian.size() < 1) {
							list_morentuijian.setContextEmptyType(true, 1);
						} else {
							list_morentuijian.setContextEmptyType(false, 1);
						}
					} catch (Exception e) {

					}
				}
			}
		});
		add(task);
	}

	void o(JSONArray array) {
		try {
			if (type == TYPE_MORENTUIJIAN) {
				/*
				 * if (CommonUtility.isNull(array_morentuijian)) {
				 * CommonUtility.putAll(array_morentuijian, array); } else {
				 */
				JSONArray arr = array;
				if (arr.length() > 0) {
					CommonUtility.putAll(array_morentuijian, arr, true);
					if (arr.length() < Constant.pageSize) {
						list_morentuijian.setHasMoreData(false);
					} else {
						list_morentuijian.setHasMoreData(true);
					}
				} else {
					list_morentuijian.setHasMoreData(false);
				}
				// }

				if (array_morentuijian.size() > 0) {
					start_qid = ((JSONObject) array_morentuijian
							.get(array_morentuijian.size() - 1)).getLong("Uid");
					// layout_img_empty.setVisibility(View.GONE);
				} else {
					// layout_img_empty.setVisibility(View.VISIBLE);
				}

				if (CommonUtility.isNull(morentuijianAdapter)) {
					morentuijianAdapter = new UserAdapter(
							(Activity) getContext(), array_morentuijian, true);
					list_morentuijian.setAdapter(morentuijianAdapter);
				} else {
					morentuijianAdapter.notifyUpdate();
				}
			} else if (type == TYPE_HAOPINGDU) {
				/*
				 * if (CommonUtility.isNull(array_haopingdu)) {
				 * CommonUtility.putAll(array_haopingdu, array); } else {
				 */
				JSONArray arr = array;
				if (arr.length() > 0) {
					CommonUtility.putAll(array_haopingdu, arr, true);
					if (arr.length() < Constant.pageSize) {
						list_morentuijian.setHasMoreData(false);
					} else {
						list_morentuijian.setHasMoreData(true);
					}
				} else {
					list_morentuijian.setHasMoreData(false);
				}
				// }

				if (array_haopingdu.size() > 0) {
					start_qid = ((JSONObject) array_haopingdu
							.get(array_haopingdu.size() - 1)).getLong("Uid");
					// layout_img_empty.setVisibility(View.GONE);
				} else {
					// layout_img_empty.setVisibility(View.VISIBLE);
				}

				if (CommonUtility.isNull(haopingduAdapter)) {
					haopingduAdapter = new UserAdapter((Activity) getContext(),
							array_haopingdu, true);
					list_morentuijian.setAdapter(haopingduAdapter);
				} else {

					haopingduAdapter.notifyUpdate();
				}
			} else if (type == TYPE_FILTER) {
				if (CommonUtility.isNull(array_filter)) {
					CommonUtility.putAll(array_filter, array, true);
				} else {
					JSONArray arr = array;
					if (arr.length() > 0) {
						CommonUtility.putAll(array_filter, arr, true);
						list_morentuijian.setHasMoreData(true);
					} else {
						list_morentuijian.setHasMoreData(false);
					}
				}

				if (array_filter.size() > 0) {
					start_qid_filter = ((JSONObject) array_filter
							.get(array_filter.size() - 1)).getLong("Uid");
					// layout_img_empty.setVisibility(View.GONE);
				} else {
					// layout_img_empty.setVisibility(View.VISIBLE);
				}

				if (CommonUtility.isNull(filterAdapter)) {
					filterAdapter = new UserAdapter((Activity) getContext(),
							array_filter, true);
					list_morentuijian.setAdapter(filterAdapter);
				} else {
					filterAdapter.notifyUpdate();

				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
		if (mDoctorList) {
			loadData(false);
		} else {
			// mTreatmentAdapter.setDataProvider(null);
			loadTreatmentData();
		}

	}

	@Override
	public void onPullUpToLoadMore(PullToRefreshBase<ListView> refreshView) {
		if (mDoctorList) {
			loadData(false);
		} else {
			loadTreatmentData();
		}
	}

	TreatmentProjectListRequest req = new TreatmentProjectListRequest();

	private void loadTreatmentData() {
		TreatmentProjectListTask task = new TreatmentProjectListTask();
		task.setRequest(req);
		task.addListener(new NetTaskListener<TreatmentProjectListTask.TreatmentProjectListRequest, TreatmentProjectListTask.TreatmentProjectListResponse>() {

			@Override
			public void onComplete(
					INetTask<TreatmentProjectListRequest, TreatmentProjectListResponse> task,
					TreatmentProjectListResponse response) {
				mListTreatments.doComplete();

				if (response != null && response.size() > 0) {
					mTreatmentAdapter.setDataProvider(response);
					mTreatmentAdapter.notifyDataSetChanged();
					// mListTreatments.setHasMoreData(false);
				}
				int size = mListTreatments.getListView().getChildCount();

				if (size < 0) {
					mListTreatments.setContextEmptyType(true, 2);
				} else {
					mListTreatments.setContextEmptyType(false, 2);
				}
			}

			@Override
			public void onException(
					INetTask<TreatmentProjectListRequest, TreatmentProjectListResponse> task,
					Exception e) {
				mListTreatments.setContextEmptyType(true, 2);
				Log.i("testnew", "onException:" + e.toString());

			}
		});
		add(task);
	}

	// private void registerReceiver() {
	// IntentFilter filter = new IntentFilter(Constant.RECIVER_QUESTION);
	// this.registerReceiver(receiver, filter);
	// }
	//
	// public void receiverRefresh(int uid, int operate) {
	// if (!CommonUtility.isNull(array_morentuijian)) {
	// refresh(uid, operate, array_morentuijian, morentuijianAdapter);
	// }
	// if (!CommonUtility.isNull(array_haopingdu)) {
	// refresh(uid, operate, array_haopingdu, haopingduAdapter);
	// }
	// }

	// private void refresh(int uid, int operate, JSONArray array,
	// DoctorAdapter adapter) {
	// for (int i = 0; i < array.length(); i++) {
	// try {
	// JSONObject object = array.getJSONObject(i);
	// if (uid == object.getJSONObject("User").getInt("Uid")) {
	// object.put("Is_Follow", operate);
	// break;
	// }
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// }
	// adapter.notifyUpdate();
	// }

	// /*
	// * (non-Javadoc)
	// *
	// * @see com.evan.common.activity.BaseActivity#onDestroy()
	// */
	// @Override
	// protected void onDestroy() {
	// super.onDestroy();
	// if (!CommonUtility.isNull(receiver))
	// this.unregisterReceiver(receiver);
	// }

	// BroadcastReceiver receiver = new BroadcastReceiver() {
	//
	// @Override
	// public void onReceive(Context context, Intent intent) {
	// int operate = intent.getIntExtra(Constant.IDENTITY_KEY_1, 0);
	// receiverRefresh(intent.getIntExtra(Constant.IDENTITY_KEY, -1),
	// operate);
	// }
	// };

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (btn_address_choose == null) {
			return;
		}
		if (!CommonUtility.isNull(data)) {
			String hospitalName = data.getStringExtra(Constant.IDENTITY_KEY);
			btn_address_choose.setText(hospitalName);
			mRequest.city = hospitalName;
		} else if (btn_address_choose.getText().toString().equals("请选择")) {
			btn_address_all.performClick();
		}
	}

}
